home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GP_MSWIN.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  19KB  |  703 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_mswin.c */
  20. /*
  21.  * Microsoft Windows 3.n platform support for Ghostscript.
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * initially created from gp_dosfb.c and gp_itbc.c 5th June 1992.
  25.  */
  26.  
  27. #include "windows_.h"
  28. #if WINVER >= 0x030a
  29. #include <shellapi.h>
  30. #endif
  31. #include "stdio_.h"
  32. #include <stdlib.h>
  33. #include <stdarg.h>
  34. #include "ctype_.h"
  35. #include "dos_.h"
  36. #include <io.h>
  37. #include "malloc_.h"
  38. #include "gp_mswin.h"
  39. #include "gp_mswtx.h"
  40.  
  41. #include "memory_.h"
  42. #include "gx.h"
  43. #include "gp.h"
  44. #include "gpcheck.h"
  45. #include "gserrors.h"
  46. #include "gxdevice.h"
  47.  
  48. #include <fcntl.h>
  49. #include <signal.h>
  50. #include "string_.h"
  51.  
  52. /* for imitation pipes */
  53. #include "stream.h"
  54. #include "filedev.h"            /* must come after stream.h */
  55. extern stream *gs_stream_stdin;        /* from zfiledev.c */
  56. extern stream *gs_stream_stdout;    /* from zfiledev.c */
  57. extern stream *gs_stream_stderr;    /* from zfiledev.c */
  58.  
  59. /* Library routines not declared in a standard header */
  60. extern char *getenv(P1(const char *));
  61.  
  62. /* Imported from gsmain.c */
  63. extern void gs_exit(P1(int exit_status));
  64.  
  65. /* Imported from gp_msdos.c */
  66. int gp_file_is_console(P1(FILE *));
  67.  
  68. /* ------ from gnuplot winmain.c plus new stuff ------ */
  69.  
  70. /* limits */
  71. #define MAXSTR 255
  72.  
  73. /* public handles */
  74. HWND hwndtext;
  75. HINSTANCE phInstance;
  76.  
  77. const LPSTR szAppName = "Ghostscript";
  78. const LPSTR szImgName = "Ghostscript Image";
  79. char FAR win_prntmp[MAXSTR];    /* filename of PRN temporary file */
  80. int win_init = 0;        /* flag to know if gp_exit has been called */
  81. int win_exit_status;
  82. BOOL is_win31 = FALSE;
  83.  
  84. /* gsview.exe */
  85. BOOL gsview = FALSE;
  86. HWND gsview_hwnd = NULL;
  87. BOOL gsview_next = FALSE;
  88. LPSTR gsview_option = "-sGSVIEW=";
  89.  
  90. /* redirected stdio */
  91. TW textwin;    /* text window structure */
  92.  
  93. /* imitation pipes */
  94. HGLOBAL pipe_hglobal = NULL;
  95. LPBYTE pipe_lpbyte = NULL;
  96. UINT pipe_count = 0;
  97.  
  98. BOOL CALLBACK _export AbortProc(HDC, int);
  99. int main(int argc, char *argv[], char *env[]);
  100.  
  101. /* our exit handler */
  102. /* also called from Text Window WM_CLOSE */
  103. void win_exit(void)
  104. {
  105. #if WINVER >= 0x030a
  106.     /* disable Drag Drop */
  107.     if (is_win31)
  108.         DragAcceptFiles(hwndtext, FALSE);
  109. #endif
  110.     /* if we didn't exit through gs_exit() then do so now */
  111.     if (win_init)
  112.         gs_exit(0);
  113.  
  114.     fcloseall();
  115.     if (win_exit_status) {
  116.         /* display message box so error messages in hwndtext can be read */
  117.         char buf[20];
  118.         sprintf(buf, "Exit code %d",win_exit_status);
  119.         MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
  120.     }
  121.     
  122.     /* tell gsview that we are closing */
  123.     if (gsview)
  124.         SendMessage(gsview_hwnd, WM_GSVIEW, GSWIN_CLOSE, (LPARAM)NULL);
  125.  
  126.     TextClose(&textwin);
  127. }
  128.  
  129. int PASCAL 
  130. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
  131. {
  132.     int i;
  133. #if defined(_MSC_VER)    /* MSC doesn't give us _argc and _argv[] so ...   */
  134. #define MAXCMDTOKENS 128
  135.     int     _argc=0;
  136.     LPSTR   _argv[MAXCMDTOKENS];
  137.     _argv[_argc] = "gswin.exe";
  138.     _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
  139.     while (_argv[_argc] != NULL)
  140.         _argv[++_argc] = _fstrtok( NULL, " ");
  141. #endif
  142.     is_win31 = FALSE;
  143.     {
  144.     WORD version = LOWORD(GetVersion());
  145.     if (((LOBYTE(version)<<8) | HIBYTE(version)) >= 0x30a)
  146.         is_win31 = TRUE;
  147.     }
  148.  
  149.     if (hPrevInstance) {
  150.         MessageBox((HWND)NULL,"Can't run twice", szAppName, MB_ICONHAND | MB_OK);
  151.         return FALSE;
  152.     }
  153.  
  154.     /* copy the hInstance into a variable so it can be used */
  155.     phInstance = hInstance;
  156.  
  157.         /* start up the text window */
  158.     textwin.hInstance = hInstance;
  159.     textwin.hPrevInstance = hPrevInstance;
  160.     textwin.nCmdShow = cmdShow;
  161.     textwin.Title = szAppName;
  162.     textwin.hIcon = LoadIcon(hInstance, "texticon");
  163.     textwin.DragPre = "(";
  164.     textwin.DragPost = ") run\r";
  165.     textwin.ScreenSize.x = 80;
  166.     textwin.ScreenSize.y = 80;
  167.     textwin.KeyBufSize = 2048;
  168.     textwin.CursorFlag = 1;    /* scroll to cursor after \n & \r */
  169.     textwin.shutdown = win_exit;
  170.  
  171.     if (TextInit(&textwin))
  172.         exit(1);
  173.     hwndtext = textwin.hWndText;
  174.  
  175.     (void) atexit((atexit_t)win_exit); /* setup exit handler */
  176.  
  177.     /* check if we are to use gsview.exe */
  178.     for (i=0; i<_argc; i++) {
  179.         if (!strncmp(_argv[i], gsview_option, strlen(gsview_option))) {
  180.         gsview_hwnd = (HWND)atoi(_argv[i]+strlen(gsview_option));
  181.         if (gsview_hwnd != (HWND)NULL) {
  182.             if (!IsWindow(gsview_hwnd)) {
  183.                 char buf[80];
  184.                 sprintf(buf,"GSVIEW window handle %u is invalid",(int)gsview_hwnd);
  185.                 MessageBox(hwndtext, buf, szAppName, MB_OK);
  186.                 return 0;
  187.             }
  188.             gsview = TRUE;
  189.             /* give gsview the handle to our text window */
  190.             SendMessage(gsview_hwnd, WM_GSVIEW, HWND_TEXT, (LPARAM)hwndtext);
  191.         }
  192.         }
  193.     }
  194.  
  195.     main(_argc, _argv, environ);
  196.  
  197.     /* never reached */
  198.     win_exit(); 
  199.     return 0;
  200. }
  201.  
  202.  
  203. BOOL CALLBACK _export
  204. AbortProc(HDC hdcPrn, int code)
  205. {
  206.     (void)gp_check_interrupts();
  207.     return(TRUE);
  208. }
  209.   
  210. /* ------ Process message loop ------ */
  211. /*
  212.  * Check messages and interrupts; return true if interrupted.
  213.  * This is called frequently - it must be quick!
  214.  */
  215. int
  216. gp_check_interrupts(void)
  217. {
  218.     TextMessage();
  219.     return 0;
  220. }
  221.  
  222. /* ====== Generic platform procedures ====== */
  223.  
  224. /* ------ Initialization/termination (from gp_itbc.c) ------ */
  225.  
  226. /* Do platform-dependent initialization. */
  227. void
  228. gp_init(void)
  229. {
  230.     win_init = 1;
  231. }
  232.  
  233. /* Do platform-dependent cleanup. */
  234. void
  235. gp_exit(int exit_status, int code)
  236. {
  237.     win_init = 0;
  238.     win_exit_status = exit_status;
  239. }
  240.  
  241. /* ------ Printer accessing ------ */
  242.   
  243. /* Forward references */
  244. private int gp_printfile(P1(char *));
  245.  
  246. /* Open a connection to a printer.  A null file name means use the */
  247. /* standard printer connected to the machine, if any. */
  248. /* Return NULL if the connection could not be opened. */
  249. FILE *
  250. gp_open_printer(char *fname, int binary_mode)
  251. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  252.     {    FILE *pfile;
  253.         pfile = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  254.             win_prntmp, "wb");
  255.         return pfile;
  256.     }
  257.     else
  258.         return fopen(fname, (binary_mode ? "wb" : "w"));
  259. }
  260.  
  261. /* Close the connection to the printer. */
  262. void
  263. gp_close_printer(FILE *pfile, const char *fname)
  264. {
  265.     fclose(pfile);
  266.     if (strlen(fname) && strcmp(fname,"PRN"))
  267.         return;        /* a file, not a printer */
  268.  
  269.     gp_printfile(win_prntmp);
  270.     unlink(win_prntmp);
  271. }
  272.  
  273. /* Windows does not provide API's in the SDK for writing directly to a */
  274. /* printer.  Instead you are supposed to use the Windows printer drivers. */
  275. /* Ghostscript has its own printer drivers, so we need to use some API's */
  276. /* that are documented only in the Device Driver Adaptation Guide */
  277. /* that comes with the DDK.  Prototypes taken from DDK <print.h> */
  278. DECLARE_HANDLE(HPJOB);
  279.  
  280. HPJOB   WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  281. int     WINAPI StartSpoolPage(HPJOB);
  282. int     WINAPI EndSpoolPage(HPJOB);
  283. int     WINAPI WriteSpool(HPJOB, LPSTR, int);
  284. int     WINAPI CloseJob(HPJOB);
  285. int     WINAPI DeleteJob(HPJOB, int);
  286. int     WINAPI WriteDialog(HPJOB, LPSTR, int);
  287. int     WINAPI DeleteSpoolPage(HPJOB);
  288.  
  289. HWND hDlgModeless;
  290.  
  291. /* Modeless dialog box - Cancel printing */
  292. BOOL CALLBACK _export
  293. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)